package com.scitc.pleasurevideo.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.util.ConstUtil;

public class DialogSettingFlowDownLoadSizeEveryDay {

    private Context context;

    private NewsPopupWindow popupWindow;

    private OnSettingComplete onSettingComplete;

    public interface OnSettingComplete{
        void onSettingComplete(int everyDaySize);
    }


    public DialogSettingFlowDownLoadSizeEveryDay(View view,Context context,OnSettingComplete onSettingComplete) {
        super();
        this.context = context;
        this.onSettingComplete=onSettingComplete;
        initView();
        showShare(view, 0, 0, 0);
    }

    public void showShare(View parent, int x, int y, int postion) {
        // 设置popwindow显示位置
        popupWindow.showAtLocation(parent, 0, x, y);
        // 获取popwindow焦点
        popupWindow.setFocusable(true);
        // 设置popwindow如果点击外面区域，便关闭。
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();
    }

    private void initView() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View popView = layoutInflater.inflate(R.layout.dialog_setting_flow_download_size_everyday, null);
        popupWindow = new NewsPopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        popupWindow.setAnimationStyle(R.style.default_dialog_show);
        popView.setOnClickListener(v -> popupWindow.dismiss());

        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        EditText etEveryDaySize= popView.findViewById(R.id.etEveryDaySize);
        etEveryDaySize.setHint(String.valueOf(ETMan.getPref(context,ConstUtil.FLOW_DOWNLOAD_SIZE_EVERY_DAY,ConstUtil.FLOW_DOWNLOAD_SIZE_DEFAULT_SIZE)));

        //确定按钮
        TextView sureBtn = popView.findViewById(R.id.tvSureBtn);
        sureBtn.setOnClickListener(v -> {
            int everyDaySizeInt=0;
            try{
                everyDaySizeInt=Integer.parseInt(etEveryDaySize.getText().toString());
                onSettingComplete.onSettingComplete(everyDaySizeInt);
                ETMan.savePref(context, ConstUtil.FLOW_DOWNLOAD_SIZE_EVERY_DAY,everyDaySizeInt);
            }catch (Exception e){
                Toast.makeText(context, "输入错误", Toast.LENGTH_SHORT).show();
            }
            popupWindow.dismiss();
        });

        //取消按钮
        TextView tvExitBtn = popView.findViewById(R.id.tvExitBtn);
        tvExitBtn.setOnClickListener(v -> popupWindow.dismiss());

    }

}
